﻿using System;

namespace HelloWorld.Code.DataAccess
{
    public class UnitOfWork : IDisposable
    {
        #region Repositories

        protected IRepository<User> UserRepository;
        public IRepository<User> GetUserRepository
        {
            get { return UserRepository ?? (UserRepository = new Repository<User>(Context)); }
        }

        #endregion

        #region UnitOfWork

        protected MvcDemoEntities Context;
        private bool _disposed;
       
        public UnitOfWork()
        {
            Context = new MvcDemoEntities();
            _disposed = false;
        }

        public void Save()
        {
            Context.SaveChanges();
        }

        public void Dispose()
        {
            if(!_disposed)
            {
                Context.Dispose();
                _disposed = true;
            }

            GC.SuppressFinalize(this);
        }

        #endregion
    }
}